package com.smsservice.activities;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.smsservice.R;
import com.smsservice.servico.ServicoEnvioSms;

/**
 * Classe principal com as opções de gravar as configurações e iniciar/parar o service de envio SMS
 * 
 * @author Vitor
 * 
 */
public class Principal extends Activity {

	private TextView lblErro = null;

	private NotificationManager mNM = null;
	
	public static final String PREFS_NAME = "ConfigServidorPref";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		lblErro = (TextView) findViewById(R.id.lblErro);

		Bundle extras = getIntent().getExtras();

		lblErro.setVisibility(View.GONE);

		mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		if (extras != null && extras.containsKey("erro") && extras.containsKey("mensagem")) {
			String msg = extras.getString("mensagem");
			boolean erro = extras.getBoolean("erro");
			int codigoNotificacao = extras.getInt("codigoNotify");
			if (erro) {
				lblErro.setVisibility(View.VISIBLE);
				lblErro.setText(msg);
				mNM.cancel(codigoNotificacao);
			}
			
		}
		

	}

	public void editarConfigServidor(View v) {
		Intent it = new Intent(this, ConfiguracaoServidor.class);
		startActivity(it);
	}

	public void iniciaServico(View v) {
		// Intent it = new Intent("INICIAR_SERVICO");

		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		String enderecoIP = settings.getString("EnderecoIP", "");
		if (enderecoIP != null && !"".equals(enderecoIP)) {
			Intent it = new Intent(this, ServicoEnvioSms.class);
			startService(it);
			finish();
		} else {
			Toast.makeText(this, "Favor preencher os dados para conexão com banco!", Toast.LENGTH_SHORT).show();
		}
		
		
	}

	public void pararServico(View v) {
		if (stopService(new Intent(this, ServicoEnvioSms.class))) {
			Toast.makeText(this, "Serviço Parado.", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "Falha ao parar Serviço de SMS.", Toast.LENGTH_SHORT).show();
		}
	}

}
